package com.avic.process.utils;

import java.util.Collections;
import java.util.Comparator;
import java.util.Vector;

import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;

import com.teamcenter.rac.ecmanagement.sorttable.*;

public class CascTableModel extends DefaultSortTableModel //implements TableModelListener
{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    boolean isCellEditable = false;
    boolean isSortable = true;
    int[] ColumnEditable = null;

    public CascTableModel()
    {
        super();
        //addTableModelListener(this);
    }

    public CascTableModel(String[] tempS, int i)
    {
        super(tempS, i);
    }

    public CascTableModel(Object[][] data, Object[] tatil)
    {
        super(data, tatil);
    }

    @SuppressWarnings("unchecked")
	public CascTableModel(Vector data, Vector tatil)
    {
        super(data, tatil);
    }

    @SuppressWarnings("unchecked")
	public void sortColumn(int i, boolean flag)
    {
        Collections.sort(getDataVector(), new ColComparator(i, flag));
    }

    @SuppressWarnings("unchecked")
	public void sortColumn(int i, int j, boolean flag)
    {
        Collections.sort(getDataVector(), new ColComparator2(i, j, flag));
    }

    public boolean isCellEditable(int i, int j)
    {
        int k;
        if(ColumnEditable != null && ColumnEditable.length > 0)
        {
            for(k = 0; k < ColumnEditable.length; k++)
            {
                if(j == ColumnEditable[k])
                {
                    return true;
                }
            }
            return isCellEditable;
        }else
        {
            return isCellEditable;
        }

    }

    public boolean isCellEditable()
    {
        return isCellEditable;
    }

    public void setColumnEditable(int[] bs)
    {
        ColumnEditable = bs;
    }

    public void setCellEditable(boolean b)
    {
        isCellEditable = b;
    }
    
    public boolean getCellEditable()
    {
        return isCellEditable;
    }

    public void setSortable(boolean b)
    {
        isSortable = b;
    }

    public boolean isSortable(int i)
    {
        return isSortable;
    }

    @SuppressWarnings("unchecked")
	class ColComparator implements Comparator
    {

        public ColComparator(int i, boolean flag)
        {
            index = i;
            ascending = flag;
        }

        public int compare(Object obj, Object obj1)
        {
            if((obj instanceof Vector) && (obj1 instanceof Vector))
            {
                Vector vector = (Vector)obj;
                Vector vector1 = (Vector)obj1;
                Object obj2 = vector.elementAt(index);
                Object obj3 = vector1.elementAt(index);

                if(obj2 == null)
                    obj2 = " ";
                if(obj3 == null)
                    obj3 = " ";

                if((obj2 instanceof Comparable) && (obj3 instanceof Comparable))
                {
                    Comparable comparable = (Comparable)obj2;
                    Comparable comparable1 = (Comparable)obj3;
                    if(ascending)
                        return comparable.compareTo(comparable1);
                    else
                        return comparable1.compareTo(comparable);
                }

            }
            return 1;
        }

        protected int index;
        protected boolean ascending;
    }

    @SuppressWarnings("unchecked")
	class ColComparator2 implements Comparator
    {

        public ColComparator2(int i, int j, boolean flag)
        {
            index = i;
            sec = j;
            ascending = flag;
        }

        public int compare(Object obj, Object obj1)
        {
            int n;
            if((obj instanceof Vector) && (obj1 instanceof Vector))
            {
                Vector vector = (Vector)obj;
                Vector vector1 = (Vector)obj1;
                Object obj2 = vector.elementAt(index);
                Object obj3 = vector1.elementAt(index);

                if(obj2 == null)
                    obj2 = " ";
                if(obj3 == null)
                    obj3 = " ";

                if((obj2 instanceof Comparable) && (obj3 instanceof Comparable))
                {
                    Comparable comparable = (Comparable)obj2;
                    Comparable comparable1 = (Comparable)obj3;
                    if(ascending)
                        n = comparable.compareTo(comparable1);
                    else
                        n = comparable1.compareTo(comparable);

                    if(n == 0)
                    {
                        return compareTo(vector.elementAt(sec), vector1.elementAt(sec));

                    }else
                    {
                        return n;
                    }
                }

            }
            return 1;
        }

        private int compareTo(Object obj2, Object obj3)
        {
            if(obj2 == null)
                obj2 = " ";
            if(obj3 == null)
                obj3 = " ";

            if((obj2 instanceof Comparable) && (obj3 instanceof Comparable))
            {
                Comparable comparable = (Comparable)obj2;
                Comparable comparable1 = (Comparable)obj3;
                if(ascending)
                    return comparable.compareTo(comparable1);
                else
                    return comparable1.compareTo(comparable);
            }else
            {
                return 1;
            }
        }

        protected int index;
        protected int sec;
        protected boolean ascending;
    }

	/*@Override
	public void tableChanged(TableModelEvent e) {
		// TODO Auto-generated method stub
		/s/System.out.println("zhangsan");
	}*/

}
